package ua.com.globallogic.training.subway;

import java.util.Calendar;
import java.util.Date;

public class Registry {
    private SubwayCard[] emittedCards;
    private StatisticsEntry[] statistics;
    private int counter = 1;
    private int statisticsCount = 0;
    private Calendar calendar;

    public static final double FARE = 4;
    public static int MAX_NUMBER_OF_CARDS = 3500000;
    public static int MAX_NUMBER_OF_STATISTICS = 10000000;

    public Registry() {
        emittedCards = new SubwayCard[MAX_NUMBER_OF_CARDS];
        statistics = new StatisticsEntry[MAX_NUMBER_OF_STATISTICS];
    }

    public Registry(SubwayCard[] emittedCards) {
        this.emittedCards = emittedCards;
    }

    //TODO: state vs Date
    public SubwayCard emitTimeBoundCard(Date expirationDate) {
//        calendar = Calendar.getInstance();
//        calendar.setTime(new Date());
//        calendar.add(Calendar.DATE, countOfDays);

        SubwayCardFactory factory = new SubwayCardFactory();
        SubwayCard newCard = factory.getSubwayCard(new TimeBoundCardState(counter, expirationDate));

        emittedCards[counter - 1] = newCard;
        counter++;
        return newCard;
    }

    //TODO: Remove code duplication
    public SubwayCard emitTripsBoundCard(int tripsCount) {
        SubwayCardFactory factory = new SubwayCardFactory();
        SubwayCard newCard = factory.getSubwayCard(new TripsBoundCardState(counter, tripsCount));
        emittedCards[counter - 1] = newCard;
        counter++;
        return newCard;
    }

    public SubwayCard emitAccumulativeCard(double balance) {
        SubwayCardFactory factory = new SubwayCardFactory();
        SubwayCard newCard = factory.getSubwayCard(new AccumulativeCardState(counter, balance));
        emittedCards[counter - 1] = newCard;
        counter++;
        return newCard;
    }

    public SubwayCard[] getEmittedCards() {
        return emittedCards;
    }

    public void addStatisticsEntry(SubwayCard card, boolean result) {
        statistics[statisticsCount].setCard(card);
        statistics[statisticsCount].setResult(result);
    }

    public Calendar getCalendar() {
        return calendar;
    }
}
